package com.meichis.mcsappframework.adapter.expandablelistview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.meichis.mcsappframework.adapter.expandablelistview.base.CommonExpandable;
import com.meichis.mcsappframework.adapter.expandablelistview.base.ViewHolder;

import java.util.List;

/**
 * Created by pei on 2016/8/4.
 */
public abstract class CommonAdapter<G extends CommonExpandable<C>, C> extends BaseExpandableListAdapter {
    private List<G> datas;
    private Context context;
    private int grouplayoutId, childlayoutId;

    public CommonAdapter(Context context, int grouplayoutId, int childlayoutId, List<G> datas) {
        this.context = context;
        this.datas = datas;
        this.grouplayoutId = grouplayoutId;
        this.childlayoutId = childlayoutId;
    }

    public void setList(List<G> mDatas) {
        datas.clear();
        datas.addAll(mDatas);
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getChilds().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getChilds().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, grouplayoutId, groupPosition);
        convertGroup(viewHolder, groupPosition, (G) (datas.get(groupPosition)), isExpanded); // G必须实现ExpandableGroup接口
        return viewHolder.getConvertView();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, childlayoutId, groupPosition);
        convertChild(viewHolder, groupPosition, (G) (datas.get(groupPosition)), childPosition, (C) (datas.get(groupPosition).getChilds().get(childPosition)), isLastChild);
        return viewHolder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    protected abstract void convertGroup(ViewHolder viewHolder, int groupPosition, G group, boolean isExpanded);

    protected abstract void convertChild(ViewHolder viewHolder, int groupPosition, G group, int childPosition, C child, boolean isLastChild);
}
